//
//  WHLoginView.m
//  SquirrelCourier
//
//  Created by 邓伟浩 on 2019/1/3.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHLoginView.h"
#import "WHMessageInputCell.h"
#import "WHCodeInputCell.h"

@interface WHLoginView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *messageTableView;
@property (nonatomic, strong) NSMutableArray *messageArray;

@end

@implementation WHLoginView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initMessageArray];
        [self setUpUI];
    }
    return self;
}

- (void)setLoginNumber:(NSString *)number {
    [_messageArray replaceObjectAtIndex:0 withObject:number];
}

- (void)initMessageArray {
    _messageArray = @[@"", @""].mutableCopy;
}

- (void)setUpUI {
    UITableView *messageTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self addSubview:messageTableView];
    
    messageTableView.delegate = self;
    messageTableView.dataSource = self;
    messageTableView.tableHeaderView = [self tableHeaderView];
    messageTableView.tableFooterView = [self tableFooterView];
    messageTableView.bounces = NO;
    messageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    messageTableView.backgroundColor = KClearColor;
    [messageTableView registerClass:[WHMessageInputCell class] forCellReuseIdentifier:NSStringFromClass([WHMessageInputCell class])];
    [messageTableView registerClass:[WHCodeInputCell class] forCellReuseIdentifier:NSStringFromClass([WHCodeInputCell class])];
    
    [messageTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    _messageTableView = messageTableView;
    
}

- (UIView *)tableHeaderView {
    UIView *tableHeaderView = [[UIView alloc] init];
    tableHeaderView.frame = CGRectMake(0, 0, KScreenWidth, kRealwidth(312));
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = IMAGE_NAMED(@"login_top");
    [tableHeaderView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(tableHeaderView);
    }];
    
    return tableHeaderView;
}

- (UIView *)tableFooterView {
    UIView *tableFooterView = [[UIView alloc] init];
    tableFooterView.frame = CGRectMake(0, 0, KScreenWidth, kRealwidth(140));
    
    CGFloat leftGap = kRealwidth(32);
    
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [tableFooterView addSubview:loginButton];
    NSString *loginTitle = @"登录";
    NSAttributedString *loginAttributeStr = [[NSAttributedString alloc] initWithString:loginTitle attributes:@{NSFontAttributeName:SYSTEMFONT(kRealwidth(17)), NSForegroundColorAttributeName:KWhiteColor}];
    [loginButton setAttributedTitle:loginAttributeStr forState:UIControlStateNormal];
    [loginButton setBackgroundImage:[UIImage imageWithColor:CMDarkOrangeColor] forState:UIControlStateNormal];
    ViewRadius(loginButton, kRealwidth(45)/2);
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(KScreenWidth-2*leftGap);
        make.centerX.mas_equalTo(tableFooterView.mas_centerX);
        make.height.mas_equalTo(kRealwidth(45));
        make.top.mas_equalTo(tableFooterView.mas_top).offset(kRealwidth(40));
    }];

    UIButton *acceptRuleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [tableFooterView addSubview:acceptRuleButton];
    [acceptRuleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(KScreenWidth-2*leftGap);
        make.centerX.mas_equalTo(tableFooterView.mas_centerX);
        make.height.mas_equalTo(kRealwidth(14));
        make.top.mas_equalTo(loginButton.mas_bottom).offset(kRealwidth(15));
    }];
    NSString *acceptText = @"登录即同意";
    NSMutableAttributedString *acceptAttrStr = [[NSMutableAttributedString alloc] initWithString:acceptText attributes:@{NSForegroundColorAttributeName:CFontContentLightGrayColor, NSFontAttributeName:SYSTEMFONT(kRealwidth(14))}];
    NSString *serviceText = @"《松鼠侠协议》";
    NSMutableAttributedString *serviceAttrStr = [[NSMutableAttributedString alloc] initWithString:serviceText attributes:@{NSForegroundColorAttributeName:CMDarkOrangeColor, NSFontAttributeName:SYSTEMFONT(kRealwidth(14))}];
    [acceptAttrStr appendAttributedString:serviceAttrStr];
    [acceptRuleButton setAttributedTitle:acceptAttrStr forState:UIControlStateNormal];
    
    [loginButton addTarget:self action:@selector(loginClicked:) forControlEvents:UIControlEventTouchUpInside];
    [acceptRuleButton addTarget:self action:@selector(acceptRule:) forControlEvents:UIControlEventTouchUpInside];
    
    return tableFooterView;
}

#pragma mark - —————— 事件 ——————

- (void)loginClicked:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(doLogin:)]) {
        [self.delegate doLogin:_messageArray];
    }
}

- (void)toForgetPasswordPage {
    if (self.delegate && [self.delegate respondsToSelector:@selector(toForgetPassword)]) {
        [self.delegate toForgetPassword];
    }
}

- (void)acceptRule:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(toRuleWeb)]) {
        [self.delegate toRuleWeb];
    }
}

#pragma mark - —————— UITableViewDelegate & UITableViewDataSource ——————

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    
    kWeakSelf(self);
    
    if (indexPath.row == 0) {
        WHMessageInputCell *messageInputCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WHMessageInputCell class]) forIndexPath:indexPath];
        
        messageInputCell.tipsImageView.image = IMAGE_NAMED(@"login_account_icon");
        [messageInputCell setInputTextPlaceHolder:@"输入账号"];
        messageInputCell.validType = ValideTypePhoneNumber;
        messageInputCell.inputTextField.secureTextEntry = NO;
        
        messageInputCell.textChangeBlock = ^(NSString *text) {
            [weakself.messageArray replaceObjectAtIndex:indexPath.row withObject:text];
        };
        messageInputCell.inputTextField.text = [_messageArray objectAtIndex:indexPath.row];
        
        cell = messageInputCell;
        
    } else if (indexPath.row == 1) {
        WHCodeInputCell *codeInputCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WHCodeInputCell class]) forIndexPath:indexPath];
        
        codeInputCell.tipsImageView.image = IMAGE_NAMED(@"login_password_icon");
        [codeInputCell setInputTextPlaceHolder:@"输入密码"];
        codeInputCell.validType = ValideTypePassword;
        codeInputCell.inputTextField.secureTextEntry = YES;
        
        codeInputCell.textChangeBlock = ^(NSString *text) {
            [weakself.messageArray replaceObjectAtIndex:indexPath.row withObject:text];
        };
        codeInputCell.inputTextField.text = [_messageArray objectAtIndex:indexPath.row];
        
        [codeInputCell.rightTipsButton setTitle:@"忘记密码" forState:UIControlStateNormal];
        
        codeInputCell.rightBtnClicked = ^{
            [weakself toForgetPasswordPage];
        };
        
        cell = codeInputCell;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kRealwidth(85);
}

@end
